package com.jprocms.module.cms.convert;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.jpro.framework.common.pojo.PageResult;
import com.jpro.framework.common.util.string.StrUtils;
import com.jprocms.module.cms.controller.portal.vo.ContentFrontRespVO;
import com.jprocms.module.cms.dal.dataobject.content.ContentCountDO;
import com.jprocms.module.cms.dal.dataobject.content.ContentDO;
import com.jprocms.module.cms.dal.dataobject.content.ContentExtDO;
import com.jprocms.module.cms.enums.EnumTimeStage;
import com.jprocms.module.cms.es.dto.ContentEsDto;
import com.jprocms.module.cms.es.dto.EsFrontSearchDto;
import com.jprocms.module.cms.es.dto.FrontSearchDto;
import org.mapstruct.IterableMapping;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Named;
import org.mapstruct.factory.Mappers;

import java.util.List;

/**
 * 内容 Convert
 *
 * @author jprocms
 */
@Mapper
public interface ContentFrontConvert {

    ContentFrontConvert INSTANCE = Mappers.getMapper(ContentFrontConvert.class);

    @Mapping(source = "contentDO.id", target = "id")
    @Mapping(source = "contentDO.createTime", target = "createTime")
    @Mapping(source = "ext.images", target = "images")
    @Mapping(source = "ext.attachs", target = "attachs")
    @Mapping(source = "ext.subtitle", target = "subtitle")
    @Mapping(source = "ext.keywords", target = "keywords")
    @Mapping(source = "ext.description", target = "description")
    @Mapping(source = "ext.author", target = "author")
    @Mapping(source = "ext.source", target = "source")
    @Mapping(source = "ext.sysImage", target = "sysImage")
    @Mapping(source = "ext.sysVideo", target = "sysVideo")
    @Mapping(source = "ext.sysAudio", target = "sysAudio")
    @Mapping(source = "ext.sysAttach", target = "sysAttach")
    @Mapping(source = "ext.sysAttachName", target = "sysAttachName")
    @Mapping(source = "ext.targetBlank", target = "targetBlank")
    @Mapping(source = "ext.text", target = "text")
    @Mapping(source = "ext.docPdf", target = "docPdf")
    @Mapping(source = "ext.docName", target = "docName")
    @Mapping(source = "ext.originDoc", target = "originDoc")
    @Mapping(source = "ext.outLink", target = "outLink")
    @Mapping(source = "ext.sysVideoDuration", target = "sysVideoDuration")
    ContentFrontRespVO convert(ContentDO contentDO, ContentExtDO ext);

    @Named(value = "convertMore")
    default ContentFrontRespVO convertMore(ContentDO bean) {
        ContentFrontRespVO respVO = convert(bean, bean.getExt());
        respVO.setCustoms(bean.getCustoms());
        respVO.setTagNames(bean.getTagNames());
        respVO.setChannelName(bean.getChannelName());
        return respVO;
    }

    @IterableMapping(qualifiedByName = "convertMore")
    List<ContentFrontRespVO> convertList(List<ContentDO> list);

    @IterableMapping(qualifiedByName = "convertMore")
    PageResult<ContentFrontRespVO> convertPage(PageResult<ContentDO> page);

    @Mapping(target = "id", source = "contentDO.id")
    @Mapping(target = "createTime", source = "contentDO.createTime")
    @Mapping(target = "creator", source = "contentDO.creator")
    @Mapping(target = "customs", source = "contentDO.customsWithoutEditor")
    @Mapping(source = "extDO.subtitle", target = "subtitle")
    @Mapping(source = "extDO.keywords", target = "keywords")
    @Mapping(source = "extDO.description", target = "description")
    @Mapping(source = "extDO.author", target = "author")
    @Mapping(source = "extDO.source", target = "source")
    @Mapping(source = "extDO.sysImage", target = "sysImage")
    @Mapping(source = "extDO.sysVideo", target = "sysVideo")
    @Mapping(source = "extDO.sysAudio", target = "sysAudio")
    @Mapping(source = "extDO.sysAttach", target = "sysAttach")
    @Mapping(source = "extDO.sysAttachName", target = "sysAttachName")
    @Mapping(source = "extDO.docPdf", target = "docPdf")
    @Mapping(source = "extDO.docName", target = "docName")
    @Mapping(source = "extDO.originDoc", target = "originDoc")
    @Mapping(source = "contentDO.ups", target = "ups")
    @Mapping(source = "contentDO.downs", target = "downs")
    @Mapping(source = "contentDO.comments", target = "comments")
    @Mapping(source = "contentDO.views", target = "views")
    @Mapping(source = "contentDO.downloads", target = "downloads")
    @Mapping(source = "contentDO.favorites", target = "favorites")
    ContentEsDto convertToEsDtoSimple(ContentDO contentDO, ContentExtDO extDO, ContentCountDO contentCountDO);

    EsFrontSearchDto convert2EsQuertDtoDef(FrontSearchDto queryDto);

    default EsFrontSearchDto convert2EsQuertDto(FrontSearchDto queryDto) {
        EsFrontSearchDto dto = convert2EsQuertDtoDef(queryDto);
        if (dto == null) {
            return dto;
        }
        dto.setKeyWords(StrUtils.splitToString(queryDto.getQ()));
        dto.setChannelIds(StrUtils.splitToLong(queryDto.getChannelIdStrs()));
        dto.setDictBlocks(StrUtils.splitToLong(queryDto.getDictBlockStrs()));
        Integer timeStage = queryDto.getTimeStage();
        EnumTimeStage stage = EnumTimeStage.getTimeStage(timeStage);
        DateTime now = DateTime.now();
        switch (stage) {
            case day:
                dto.setReleaseTimeStart(DateUtil.beginOfDay(now).toJdkDate());
                break;
            case week:
                dto.setReleaseTimeStart(DateUtil.beginOfWeek(now).toJdkDate());
                break;
            case month:
                dto.setReleaseTimeStart(DateUtil.beginOfMonth(now).toJdkDate());
                break;
            case year:
                dto.setReleaseTimeStart(DateUtil.beginOfYear(now).toJdkDate());
                break;
            default:
        }
        return dto;
    }

}
